package com.example.backend.service.Impl.user.account;
import com.example.backend.Service.Impl.Utils.UserDetailsImpl;
import com.example.backend.pojo.Kobuser;
import com.example.backend.service.user.account.LoginService;
import com.example.backend.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private AuthenticationManager authenticationManager;

    @Override
    public Map<String, String> getToken(String username, String password) {
        UsernamePasswordAuthenticationToken authenticationToken =
                new UsernamePasswordAuthenticationToken(username, password);

        Authentication authentication = authenticationManager.authenticate(authenticationToken);
        UserDetailsImpl loginUser = (UserDetailsImpl) authentication.getPrincipal();
        Kobuser user = loginUser.getUser();
        String jwt = JwtUtil.createJWT(user.getUserid().toString());
        Map<String, String> map = new HashMap<>();
        map.put("ERROR_MSG", "SUCCESS");
        map.put("token", jwt);
        return map;
    }
}
